package com.gqz.springbootdemo.controller;

import com.gqz.springbootdemo.mapper.CourseMapper;
import com.gqz.springbootdemo.pojo.Course;
import com.gqz.springbootdemo.pojo.Student;
import com.gqz.springbootdemo.service.CourseService;
import com.gqz.springbootdemo.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p></p>
 *
 * @author gqz20
 * @create 2022-05-05 15:05
 **/
@Controller
@RequestMapping("stu")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseMapper courseMapper;

    /**
     * List.
     *
     * @param id    the id
     * @param model the model
     * @return the string
     */
    @GetMapping("list")
    public String list(@RequestParam(required = false) Long id, Model model) {
        List<Student> students = studentService.findList(id);
        model.addAttribute("students", students);
        return "student";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        //获取所有的course数据
        List<Course> courses = courseService.list();
        model.addAttribute("courses", courses);
        return "/add";
    }

    @PostMapping("add")
    public ResponseEntity<Object> add(
            @RequestParam String name,
            @RequestParam String sex,
            @RequestParam List<String> hobby,
            @RequestParam List<Integer> courses
    ) {
        Student student = new Student();
        student.setName(name);
        student.setSex(sex);
        student.setHobby(StringUtils.join(hobby, ","));

        List<Course> courseList = courses.stream().map(id -> new Course().setId(id)).collect(Collectors.toList());
        student.setCourses(courseList);


        studentService.addStudent(student);
        return ResponseEntity.ok("add student success");
    }

    /**
     * 到修改页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer sid, Model model) {
        List<Student> students = studentService.findList(Long.valueOf(sid));
        if (students.size() > 1) {
            throw new RuntimeException("根据学生id查找学生异常");
        }
        //2、放入作用域
        model.addAttribute("student", students.get(0));

        List<Course> courses = courseService.list();
        model.addAttribute("courses", courses);

        return "update";
    }

    @PutMapping("update")
    public ResponseEntity<Object> update(
            @RequestParam Integer id,
            @RequestParam String name,
            @RequestParam String sex,
            @RequestParam List<String> hobby,
            @RequestParam List<Integer> courses) {
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setHobby(StringUtils.join(hobby, ","));

        List<Course> courseList = courses.stream().map(i -> new Course().setId(i)).collect(Collectors.toList());
        student.setCourses(courseList);

        studentService.updateStudent(student);
        return ResponseEntity.ok("update student success");
    }


    @GetMapping("getAll")
    public ResponseEntity<Course> getAll(){
        Course course = new Course();
        course.setName("1,3");
        System.out.println(courseMapper.getAll(course));
        return ResponseEntity.ok(course);
    }
}

